package edu.nf.project.services.employeeAndCustomerInformation.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.CustomerInfo;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.convention.Member;
import edu.nf.project.entitys.enums.employeeAndCustomerInformation.MemberIdEnum;
import edu.nf.project.services.exception.derivedAnomalies.MemberException;
import edu.nf.project.mappers.employeeAndCustomerInformation.convention.CustomerInfoMapper;
import edu.nf.project.mappers.employeeAndCustomerInformation.convention.MemberMapper;
import edu.nf.project.services.employeeAndCustomerInformation.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员服务实现类
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private CustomerInfoMapper customerInfoMapper;

    @Override
    public Page<Member> getMemberList(Page<Member> page) {
        return memberMapper.selectPage(page, null);
    }

    @Override
    public Member getMemberInfo(Integer memberId) {
        Member member = memberMapper.selectById(memberId);
        if (member == null) {
            throw new MemberException(50001, "会员等级不存在");
        }
        return member;
    }

    @Override
    @Transactional
    public boolean updateCustomerMemberLevel(Integer customerId, Integer memberId) {
        // 检查会员等级是否存在
        Member member = memberMapper.selectById(memberId);
        if (member == null) {
            throw new MemberException(50001, "会员等级不存在");
        }

        // 检查用户是否存在
        CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);
        if (customerInfo == null) {
            throw new MemberException(50003, "用户不存在");
        }

        // 更新用户会员等级
        CustomerInfo updateInfo = new CustomerInfo();
        updateInfo.setCustomerId(customerId);
        // 根据传入的memberId设置对应的会员等级
        updateInfo.setMemberId(MemberIdEnum.values()[memberId]);

        int result = customerInfoMapper.updateById(updateInfo);
        
        return result > 0;
    }
} 